package com.example.text4;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.FileDescriptor;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class Myadapter extends BaseAdapter {
    List<Data> list;

    public Myadapter(List<Data> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = View.inflate(parent.getContext(), R.layout.activity_item, null);
        ImageView image = view.findViewById(R.id.image);
        TextView sing = view.findViewById(R.id.sing);
        TextView player = view.findViewById(R.id.player);
        TextView time = view.findViewById(R.id.time);
        image.setImageResource(R.drawable.music3);
        sing.setText(list.get(position).getSing());
        player.setText(list.get(position).getPlayer());
        time.setText(Time(list.get(position).getTime()));
        Bitmap bitmap = null;
        bitmap = getArtworkFromFile(parent.getContext(), list.get(position).getId());
        if (bitmap!=null){
            image.setImageBitmap(bitmap);
        }
        return view;
    }
    //根据歌曲id，获取其封面图片
    public static Bitmap getArtworkFromFile(Context context, long songid) {
        Bitmap bm = null;
        if (songid < 0) {
            throw new IllegalArgumentException("Must specify an album or a song id");
        }
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            FileDescriptor fd = null;
            Uri uri = Uri.parse("content://media/external/audio/media/" + songid + "/albumart");
            ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
            if (pfd != null) {
                fd = pfd.getFileDescriptor();
            }
            options.inSampleSize = 1;
            // 只进行大小判断
            options.inJustDecodeBounds = true;
            // 调用此方法得到options得到图片大小
            BitmapFactory.decodeFileDescriptor(fd, null, options);
            // 我们的目标是在800pixel的画面上显示
            // 所以需要调用computeSampleSize得到图片缩放的比例
            options.inSampleSize = computeSampleSize(options,80);
            // 我们得到了缩放的比例，现在开始正式读入Bitmap数据
            options.inJustDecodeBounds = false;
            options.inDither = false;
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            //根据options参数，减少所需要的内存
            bm = BitmapFactory.decodeFileDescriptor(fd, null, options);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bm;
    }
    /**
     对图片进行合适的缩放
     @param options
     @param target
     @return
     */
    public static int computeSampleSize(BitmapFactory.Options options, int target) {
        int w = options.outWidth;
        int h = options.outHeight;
        int candidateW = w / target;
        int candidateH = h / target;
        int candidate = Math.max(candidateW, candidateH);
        if (candidate == 0) {
            return 1;
        }
        if (candidate > 1) {
            if ((w > target) && (w / candidate) < target) {
                candidate -= 1;
            }
        }
        return candidate;
    }
    public String Time(Long hm){
        SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
        return sdf.format(hm);
    }
}
